package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"io-demo/src/entity"
	"os"
	"strconv"
	"strings"
)

func main() {

}

func print() {
	fmt.Println("hello world")
}

func readFile() {
	var config entity.ConfigInfo

	path := "F:\\baiduyun\\FIXED_WDEP_理财产品中央数据交换协议_V1.0.ini"

	file, err := os.Open(path)
	if err != nil {
		panic(err)
	}
	defer file.Close()
	scanner := bufio.NewScanner(file)

	var label string
	var index, count int
	var seq int = -1
	var flag bool

	for scanner.Scan() {
		line := scanner.Text()
		if line == "" || line[0] == ';' {
			continue
		}
		if line[0] == '[' {
			temp := line[1 : len(line)-1]
			if temp != label || (seq != -1 && config.HeadInfo[seq].Pattern == temp) {
				flag = true
				count = 0
			}
			label = temp
		} else {
			if label == "AuthorInformation" {
				split := strings.Split(line, "=")
				if index == 0 {
					config.AuthorInfo.Author = split[1][1 : len(split[1])-1]
				} else if index == 1 {
					config.AuthorInfo.Describe = split[1][1 : len(split[1])-1]
				} else if index == 2 {
					config.AuthorInfo.Email = split[1][1 : len(split[1])-1]
				}
				index++
			} else if label == "TipDictionary" {
				split := strings.Split(line, "=")
				value := strings.Split(split[1][1:], ":")
				tipDict := entity.TipDictionary{Seq: split[0], Tip: value[0], Describe: value[1][:len(value[1])-1]}
				config.TipDictionary = append(config.TipDictionary, tipDict)
			} else if label == "Dictionary" {
				split := strings.Split(line, "=")
				value := strings.Split(split[1][1:], "@")
				dict := strings.Split(value[1][:len(value[1])-1], "|")
				var objDict entity.Dictionary
				objDict.Seq = split[0]
				objDict.Dict = value[0]
				for _, temp := range dict {
					if temp == "" {
						continue
					}
					arr := strings.Split(temp, ":")
					if len(arr) < 2 {
						arr = strings.Split(temp, ".")
					}
					item := entity.DictItem{Key: arr[0], Value: arr[1]}
					objDict.ItemList = append(objDict.ItemList, item)
				}
				config.Dictionary = append(config.Dictionary, objDict)
			} else {
				// 下一个label
				array := strings.Split(line, "=")
				if flag {
					var headInfo entity.HeadInfo
					headInfo.Pattern = label
					headInfo.BaseInfo = make(map[string]string)
					headInfo.BaseInfo[array[0]] = array[1]
					config.HeadInfo = append(config.HeadInfo, headInfo)
					count++
					seq++
					flag = false
				} else {
					if array[0] == "fieldprimarylist" {
						config.HeadInfo[seq].BaseInfo[array[0]] = array[1][1 : len(array[1])-1]
					} else if count < 14 {
						config.HeadInfo[seq].BaseInfo[array[0]] = array[1]
						count++
					} else {
						var field entity.FieldInfo
						field.Seq = array[0]
						arr := strings.Split(array[1], ",")
						field.FieldType = arr[0]
						length, _ := strconv.Atoi(arr[1])
						field.Length = length
						p, _ := strconv.Atoi(arr[2])
						field.Points = p
						field.Name = arr[3]
						field.Value = arr[4]
						config.HeadInfo[seq].Data = append(config.HeadInfo[seq].Data, field)
					}
				}

			}

			//fmt.Println("\t", line)
		}
	}

	//fmt.Println(config.AuthorInfo)
	//fmt.Println(config.TipDictionary)
	//fmt.Println(config.Dictionary)
	//fmt.Println(config.HeadInfo)

	// 将Person对象转换为JSON格式的数据
	//b, err := json.Marshal(config)
	b, err := json.MarshalIndent(config, "", "  ")
	if err != nil {
		fmt.Println("对象转换为JSON格式失败：", err)
	}
	fmt.Println(string(b))

	target := "F:\\baiduyun\\FIXED_WDEP_理财产品中央数据交换协议_V1.0.json"
	f, err := os.Create(target)

	if err != nil {
		fmt.Println("创建文件失败：", err)
		return
	}
	defer f.Close()

	_, err = f.Write(b)
	if err != nil {
		fmt.Println("写入文件失败：", err)
		return
	}

	if err := scanner.Err(); err != nil {
		panic(err)
	}
}
